<?php

//命名空间
namespace Home\Controller;

//导入类
use Think\Controller;
use Think\Think;

//User 控制器
require_once(THINK_PATH.'Library/Vendor/PHPMailer/class.phpmailer.php');
class UserController extends Controller
{
    public function yzm()
    {
        $Verify = new \Think\Verify();
        $Verify->entry();
    }

    public function reg()
    {
        $this->display();
    }

    public function login()
    {
        $this->display();
    }

    //退出
    public function loginOut()
    {
        $_SESSION['qzone']['home'] = null;
        $this->success('退出成功！', U('User/login'));
    }

    //验证注册的信息
    public function verifyReg()
    {
        $user = D('User');
        if (!$user->create()) {
            $this->ajaxReturn(array('status' => 1, 'message' => $user->getError()));
        } else {
            $this->ajaxReturn(array('status' => 0));
        }
    }

    public function startReg()
    {
        //先验证验证码
        $Verify = new \Think\Verify();
        if (!$Verify->check($_POST['vcode'])) {
            $this->ajaxReturn(array('status' => 1, 'message' => array('vcode' => '验证码不正确!')));
        }
        $user = D('User');
        if (!$user->create()) {
            $this->ajaxReturn(array('status' => 1, 'message' => $user->getError()));
        } else {
            $user->passwd = md5($user->passwd);
            $user->ctime = time();
            $user->add();
            $this->ajaxReturn(array('status' => 0));
        }
    }
public function login1(){

       $Verify = new \Think\Verify();
 $m=M('User');
 $arr['username']=$_POST['username'];
 $arr['passwd']=md5($_POST['passwd']);
 $data=$m->where("username={$arr['username']} AND `login`='0'")->find();
// $data['login']=$login;


 if($data!=null  )
 {
    if(!$Verify->check($_POST['vcode']) )
        {
            $this->ajaxReturn(array('status'=>1,'message'=>'验证码不正确!'));
        }else{
        // $this->success('登录成功!','../Index/index');
        $_SESSION['qzone']['home']['username'] = $data['username'];
        $_SESSION['qzone']['home']['id'] = $data['id'];
        $_SESSION['qzone']['home']['zonedp'] = $data['zonedp'];

         //登录成功，写session，跳转到首页
        if($_POST['rememberMe'] === '0')
        {
            //需要记住密码，往cokkie写数据
            cookie('qzone',array('homeUserName'=>$data['username'],'homePassWd'=>md5($data['passwd'])),604800);
        }
        // $_SESSION['uid']
         $this->ajaxReturn(array('status'=>0)); 

    }
 }else{
     # code...
 }{
    // $this->error('用户名或者密码错误！');
    $this->ajaxReturn(array('status'=>2,'message'=>'不能登陆！'));
 }


}




    // public function login1()
    // {

    //     $Verify = new \Think\Verify();
    //     $m = M('User');
    //     $arr['username'] = $_POST['username'];
    //     $arr['passwd'] = md5($_POST['passwd']);
    //     $data = $m->where($arr)->find();
    //     if ($data != null) {
    //         if (!$Verify->check($_POST['vcode'])) {
    //             $this->ajaxReturn(array('status' => 1, 'message' => '验证码不正确!'));
    //         } else {
    //             // $this->success('登录成功!','../Index/index');
    //             $_SESSION['qzone']['home']['username'] = $data['username'];
    //             $_SESSION['qzone']['home']['id'] = $data['id'];
    //             $_SESSION['qzone']['home']['zonedp'] = $data['zonedp'];
    //             //登录成功，写session，跳转到首页
    //             if ($_POST['rememberMe'] === '0') {
    //                 //需要记住密码，往cokkie写数据
    //                 cookie('qzone', array('homeUserName' => $data['username'], 'homePassWd' => md5($data['passwd'])), 604800);
    //             }
    //             // $_SESSION['uid']
    //             $this->ajaxReturn(array('status' => 0));

    //         }
    //     } else {
    //         // $this->error('用户名或者密码错误！');
    //         $this->ajaxReturn(array('status' => 2, 'message' => '用户名或者密码错误！'));
    //     }


    // }

    public function index()
    {
        $data = M('user')->order('id desc')->select();
        $this->assign('title', '用户列表');
        $this->assign('list', $data);
        $this->display();
    }

    public function forgetPasswd()
    {
        $email = I('post.email');
        $model = M('User');
        $result = $model->field('id')->where("email='{$email}'")->find();
        if($result==null)
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '此邮箱并没有在本网站上注册！'));
        }
        //开始发送邮件
        $code = str_shuffle('1234567890');
        $url = 'http://localhost'.U('User/changPasswd',array('code'=>$code,'email'=>$email));
        $sendResult = sendMail('qzone项目组',$email,'重置密码',"<a href='$url'>点击重置</a>重置链接5分钟内有效");
        if($sendResult)
        {
            //发送成功，开始写入数据库
            $model = M('Email');
            $data['code'] = $code;
            $data['email'] = $email;
            $data['ctime'] = time();
            $model->create($data);
            $result = $model->add();
            if($result > 0)
            {
                $this->ajaxReturn(array('status' => 0, 'message' => '发送成功，请点击邮箱里的重置链接进行重置！'));
            }else
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '发送失败，请联系管理员！'));
            }
        }else
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '发送失败，请联系管理员！'));
        }
    }

    public function changPasswd()
    {
        if(IS_AJAX)
        {
            $email = I('post.email');
            $code = I('post.code');
            $passwd = md5(I('post.passwd'));
            $model = M('Email');
            $data = $model->where("email='{$email}' AND code='{$code}'")->find();
            if($data == null)
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '密码修改失败！'));
            }else{
                $result = M('User')->where("email='{$email}'")->save(array('passwd'=>$passwd));
                $this->ajaxReturn(array('status' => 0, 'message' => '密码修改成功！'));
            }
        }
        $email = I('get.email');
        $code = I('get.code');
        $time = time()-300;
        $model = M('Email');
        $data = $model->where("email='{$email}' AND code='{$code}' AND ctime>='{$time}'")->find();
        if($data == null)
        {
            $this->error('验证不正确或链接失效，请重新发送邮件！',U('Index/index'));
        }else{
            $this->assign('email', $email);
            $this->assign('code', $code);
            $this->display();
        }
    }
    public function addPengyou()
    {
        if(IS_AJAX)
        {
            $data['who_id'] = I('post.uid');
            $data['uid'] = $_SESSION['qzone']['home']['id'];
            $data['ctime'] = time();
            $model = M('Friends');
            if($model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}'")->find() != null)
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '已经添加为为好友'));
            }
            $model->create($data);
            $result = $model->add();
            if($result>0)
            {
                $this->ajaxReturn(array('status' => 0, 'message' => '添加成功！'));
            }else
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '添加失败！'));
            }

        }else
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法数据来源'));
        }
    }
    public function addAttention()
    {
        if(IS_AJAX)
        {
            $model = M('Friends');
            $data['who_id'] = I('post.uid');
            $data['uid'] = $_SESSION['qzone']['home']['id'];
            if($model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}'")->find() == null)
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '请先把对方加为好友再添加关心'));
            }
            $result = $model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}'")->save(array('is_attention'=>1));
            if($result>0)
            {
                $this->ajaxReturn(array('status' => 0, 'message' => '特别关心成功'));
            }else
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '特别关心失败'));
            }

        }else
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法数据来源'));
        }
    }
    public function cancelPengyou()
    {
        if(IS_AJAX)
        {
            $model = M('Friends');
            $data['who_id'] = I('post.uid');
            $data['uid'] = $_SESSION['qzone']['home']['id'];
            if($model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}' AND is_attention<>1")->find() == null)
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '没有好友关系或已设置为特别关心'));
            }
            $result = $model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}'")->delete();
            if($result>0)
            {
                $this->ajaxReturn(array('status' => 0, 'message' => '取消好友关系成功'));
            }else
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '取消好友关系失败'));
            }

        }else
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法数据来源'));
        }
    }
    public function cancelAttention()
    {
        if(IS_AJAX)
        {
            $model = M('Friends');
            $data['who_id'] = I('post.uid');
            $data['uid'] = $_SESSION['qzone']['home']['id'];
            if($model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}' AND is_attention=1")->find() == null)
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '没有好友关系或尚未设置为特别关心'));
            }
            $result = $model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}'")->save(array('is_attention'=>0));
            if($result>0)
            {
                $this->ajaxReturn(array('status' => 0, 'message' => '取消关心成功'));
            }else
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '取消关心失败'));
            }

        }else
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法数据来源'));
        }
    }
    //查看好友关系的级别
    public function pengyouLevel()
    {
        if(IS_AJAX)
        {
            $model = M('Friends');
            $data['who_id'] = I('post.uid');
            $data['uid'] = $_SESSION['qzone']['home']['id'];
            $result = $model->where("uid='{$data['uid']}' AND who_id='{$data['who_id']}'")->find();
            if($result == null)
            {
                $this->ajaxReturn(array('status' => 0, 'message' => '没有关系'));
            }elseif($result['is_attention'] == 0)
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '好友关系'));
            }elseif($result['is_attention'] == 1)
            {
                $this->ajaxReturn(array('status' => 2, 'message' => '特别关心'));
            }

        }else
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法数据来源'));
        }
    }

}
